package com.lagou.client;

import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;

/**
 * 编程实现管理员系统的功能
 * @author cz
 *
 */
public class ClientManageView {
    // 使用合成复用原则
    private ClientInitClose cic;

    /**
     * 自定义构造方法实现成员变量的初始化
     * @param cic
     */
    public ClientManageView(ClientInitClose cic) {
        this.cic = cic;
    }

    /**
     * 自定义成员方法实现管理主界面的绘制
     * @throws InterruptedException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void clientManagePage() throws InterruptedException, IOException, ClassNotFoundException {
        while(true) {
            System.out.println("\n\n\t    欢迎进入管理员系统");
            System.out.println("------------------------------------------");
            System.out.print("   [1] 学员管理");
            System.out.println("     [2] 考题管理");
            System.out.println("   [0] 退出系统");
            System.out.println("------------------------------------------");
            System.out.println("请选择要进入的管理系统：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose) {
                case 1: clientUserManagePage(); break;
                case 2: clientQuestionManagePage(); break;
                case 0: System.out.println("正在退出管理系统...");
                    Thread.sleep(5000);
                    return;
                default: System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 自定义成员方法实现学员管理界面的绘制
     * @throws InterruptedException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void clientUserManagePage() throws InterruptedException, IOException, ClassNotFoundException {
        while(true) {
            System.out.println("\n\n\t    欢迎进入学员系统");
            System.out.println("------------------------------------------");
            System.out.print("   [1] 增加学员");
            System.out.println("     [2] 删除学员");
            System.out.print("   [3] 修改学员");
            System.out.println("     [4] 查找学员");
            System.out.println("   [0] 退出系统");
            System.out.println("------------------------------------------");
            System.out.println("请选择要进行的操作：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose) {
                case 1: clientAddUser(); break;
                case 2: System.out.println("正在删除学员..."); break;
                case 3: System.out.println("正在修改学员..."); break;
                case 4: System.out.println("正在查找学员..."); break;
                case 0: System.out.println("正在退出学员管理系统...");
                    Thread.sleep(5000);
                    return;
                default: System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 自定义成员方法实现客户端增加学员账户的功能
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void clientAddUser() throws IOException, ClassNotFoundException {
        System.out.println("请输入要增加的学员账户名称：");
        String strName = ClientScanner.getScanner().next();
        // 构造UserMessage类型的对象并使用对象输出流发送给服务器
        UserMessage tum = new UserMessage("clientAddUser",
                new User(strName, "123456"));
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送增加的账户信息成功！");
        // 接收服务器发来的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if("success".equals(tum.getType())) {
            System.out.println("增加学员账户成功！");
        } else {
            System.out.println("增加学员账户失败！");
        }
    }

    /**
     * 自定义成员方法实现考题管理界面的绘制
     * @throws InterruptedException
     */
    public void clientQuestionManagePage() throws InterruptedException {
        while(true) {
            System.out.println("\n\n\t    欢迎进入考题系统");
            System.out.println("------------------------------------------");
            System.out.print("   [1] 增加考题");
            System.out.println("     [2] 删除考题");
            System.out.print("   [3] 修改考题");
            System.out.println("     [4] 查找考题");
            System.out.println("   [0] 退出系统");
            System.out.println("------------------------------------------");
            System.out.println("请选择要进行的操作：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose) {
                case 1: System.out.println("正在增加考题..."); break;
                case 2: System.out.println("正在删除考题..."); break;
                case 3: System.out.println("正在修改考题..."); break;
                case 4: System.out.println("正在查找考题..."); break;
                case 0: System.out.println("正在退出考题管理系统...");
                    Thread.sleep(5000);
                    return;
                default: System.out.println("输入错误，请重新选择！");
            }
        }
    }
}
